#!/usr/bin/env python3
"""
通过__iter__和__next__实现了一个可输出指定数字范围内奇数的类
"""


class OddNumber:
    def __init__(self, end, start=1):
        self.max_num = end
        self.start = start
        self.first = True

    def __iter__(self):
        return self

    def __next__(self):
        if self.first:               #第一次调用返回起始值
            self.first = False
            if self.start % 2 == 0:  #起始值若非奇数，给其加1并返回
                self.start += 1
            return self.start
        else:
            self.start += 2
            if self.start < self.max_num:
                return self.start
            else:
                raise StopIteration  #遍历超出给定范围抛出StopIteration停止迭代


if __name__ == '__main__':
    odd = OddNumber(30, 13)
    for num in odd:
        print(num)
